package Client端;

import javax.swing.*;
import java.awt.*;

public class inputFrame2 extends JFrame {
    public final static Font font_20 = new Font("", Font.PLAIN, 20);
    private final JLabel[] labels = new JLabel[2];
    private final JTextField[] inputs = new JTextField[2];
    private final String[] names = new String[]{"群号：", "群名："};

    public inputFrame2() {
        int width = 300;
        int height = 220;
        setSize(width, height);
        setLocationRelativeTo(null);
        setLayout(null);
        for (int i = 0; i < 2; i++) {
            labels[i] = new JLabel(names[i]);
            labels[i].setFont(font_20);
            labels[i].setBounds(10, 10 + 40 * i, 80, 30);
            add(labels[i]);
            inputs[i] = new JTextField();
            inputs[i].setFont(font_20);
            inputs[i].setBounds(100, 10 + 40 * i, 180, 30);
            add(inputs[i]);
        }
        JLabel tip = new JLabel("测试", JLabel.CENTER);
        tip.setBounds(40, 100, 200, 20);
        tip.setFont(font_20);
        add(tip);

        JButton submit = new JButton("提交");
        submit.setBounds(30, 140, 100, 30);
        submit.setFont(font_20);
        JButton reset = new JButton("重置");
        reset.setBounds(160, 140, 100, 30);
        reset.setFont(font_20);
        add(submit);
        add(reset);

        reset.addActionListener(e -> {
            for (JTextField input : inputs) input.setText("");
        });
        submit.addActionListener(e -> {
            String name = inputs[1].getText();
            String gid = inputs[0].getText();
            if (gid == null) tip.setText("群号不能为空");
            else if (name == null) tip.setText("群名不能为空");
            else if (gid.length() != 9) tip.setText("群号必须是9位");
            else if (!ClientDeal.createGroup(name, gid)) tip.setText("群号已存在");
            else setVisible(false);
        });
    }

    public void wake() {
        for (JTextField input : inputs) input.setText("");
        setVisible(true);
    }
}
